package com.superzqb.asus.superzqbFragment.superzqbSave;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.baoyz.widget.PullRefreshLayout;
import com.superzqb.asus.Api.SaveApi;
import com.superzqb.asus.superzqb.R;
import com.superzqb.asus.superzqbFragment.superzqbSave.Adapter.GoodsListAdapter;
import com.superzqb.asus.superzqbFragment.superzqbSave.Bean.GoodsListBean;
import com.superzqb.asus.superzqbUtil.Url;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class StagingAssistant extends Activity implements View.OnClickListener{
    private ImageView back,add;
    private SharedPreferences sp;
    private PullRefreshLayout layout;
    private GoodsListAdapter adapter;
    private List<GoodsListBean> listAll;
    private ListView lv;
    private LinearLayout toShop;
    private int lastItem;
    private int listSize;
    private int pagenum=1;
    int uid;
    public static boolean flag=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.staging_assistant);
        initView();
        listAll=new ArrayList<>();
        adapter = new GoodsListAdapter(StagingAssistant.this, listAll);
        lv.setAdapter(adapter);
        layout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                layout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        layout.setRefreshing(false);
                        listAll=new ArrayList<>();
                        adapter = new GoodsListAdapter(StagingAssistant.this, listAll);
                        lv.setAdapter(adapter);
                        getLv();
                    }
                }, 2000);
            }
        });
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id){
                // TODO Auto-generated method stub
                adapter.changeColor(position);
                adapter.notifyDataSetInvalidated();
                GoodsListBean item= (GoodsListBean)parent.getItemAtPosition(position);

            }
        });
        lv.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == 0) {
                    if (lastItem == listSize) {
                        pagenum++;
                        Toast.makeText(StagingAssistant.this,
                                "加载第" + pagenum + "页",
                                Toast.LENGTH_SHORT).show();
                        getLv();
                    }
                }
            }
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
                lastItem = lv.getLastVisiblePosition();

            }
        });
    }
    private void initView() {
        sp=getSharedPreferences("uinfo",GoodsList.MODE_PRIVATE);
        uid=Integer.valueOf(sp.getString("uid",null));
        layout= (PullRefreshLayout)findViewById(R.id.swipeRefreshLayout);
        back=(ImageView)findViewById(R.id.back);
        add=(ImageView)findViewById(R.id.add);
        lv=(ListView)findViewById(R.id.listView);
        toShop=(LinearLayout)findViewById(R.id.toShop);

        back.setOnClickListener(this);
        add.setOnClickListener(this);
        toShop.setOnClickListener(this);
    }
    private void getLv() {
        Retrofit retrofit= new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(new Url().getBase_url())
                .build();
        SaveApi api=retrofit.create(SaveApi.class);
        Call<List<GoodsListBean>> call=api.getBuyFinishAdd(1,uid);
        call.enqueue(new Callback<List<GoodsListBean>>() {
            @Override
            public void onResponse(Call<List<GoodsListBean>> call, Response<List<GoodsListBean>> response) {
                if(response.isSuccessful()){
                    List<GoodsListBean> list=response.body();
                    if(list!=null) {
                        listAll.addAll(list);
                        listSize = listAll.size() - 1;
                        adapter.setList(listAll);
                        adapter.notifyDataSetChanged();
                    }
                }

            }

            @Override
            public void onFailure(Call<List<GoodsListBean>> call, Throwable throwable) {
                  Toast.makeText(StagingAssistant.this,"正在查找，请稍后",Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.add:
                Intent intent=new Intent(StagingAssistant.this, GoodsList.class);
                startActivity(intent);
                break;
            case R.id.toShop:
                flag=true;
                finish();
                break;
            default:break;
        }
    }

    public static boolean isFlag() {
        return flag;
    }

    public static void setFlag(boolean flag) {
        StagingAssistant.flag = flag;
    }

    @Override
    protected void onResume() {
        super.onResume();
        listAll.clear();
        getLv();
    }
}
